package com.hmall.item.web;


import com.hmall.item.pojo.Item;
import com.hmall.item.pojo.PageResult;
import com.hmall.item.service.IItemService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;

@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;
    /**
    * @Auth: wangxinglong
    * @desc:分页查询
    * @Date: 2023/6/27 17:10
    */
    @GetMapping("list")
    public PageResult page(@Param("page") Integer page,
                              @Param("size") Integer size){
         PageResult pageResult=itemService.pagesearch(page,size);
        //return itemService.findAll(page,size);
        return pageResult;
    }
    /**
    * @Auth: wangxinglong
    * @desc:根据ID查询物品
    * @Date: 2023/6/27 17:21
    */
    @GetMapping("/{id}")
    public Item selectById(@PathVariable Integer id){
        Item item= itemService.selectByid(id);
        return item;
    }
    /**
    * @Auth: wangxinglong
    * @desc:添加物品
    * @Date: 2023/6/27 17:22
    */
    @PostMapping
    public void saveItem(@RequestBody Item item) throws Exception{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        Date creatDate=simpleDateFormat.parse(format);
        item.setCreateTime(creatDate);
        item.setUpdateTime(creatDate);
        itemService.save(item);
    }
    /**
    * @Auth: wangxinglong
    * @desc:商品上/下货
    * @Date: 2023/6/27 17:51
    */
    @PutMapping("/status/{id}/{status}")
    private void changeStatus(@PathVariable Integer id,
                              @PathVariable Integer status){
        itemService.change(id,status);
    }
    /**
    * @Auth: wangxinglong
    * @desc:修改物品
    * @Date: 2023/6/27 17:22
    */
    @PutMapping
    public void update(@RequestBody Item item) throws Exception{
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = simpleDateFormat.format(new Date());
        Date upDate=simpleDateFormat.parse(format);
        item.setUpdateTime(upDate);//TODO 更新日期未完成
        itemService.updateById(item);
    }
    /**
    * @Auth: wangxinglong
    * @desc:根据ID删除
    * @Date: 2023/6/27 17:41
    */
    @DeleteMapping("/{id}")
    public void delete(@PathVariable Integer id){
        itemService.removeById(id);
    }
    /**
    * @Auth: wangxinglong
    * @desc:减库存接口
    * @Date: 2023/6/30 9:16
    */
    @PutMapping("/stock/{itemId}/{num}")
    public void reduce(@PathVariable("itemId") Long itemId,
                       @PathVariable("num") Long num){
        itemService.redece(itemId,num);
    }
}
